package com.amc.wxSend.service.impl;

import com.amc.wxSend.domain.SysDictData;
import com.amc.wxSend.service.ISysDictDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * html调用 thymeleaf 实现字典读取
 *
 * @author ZhangXX
 */
@Service("dict")
@Slf4j
public class DictService {
    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 根据字典类型查询字典数据信息
     *
     * @param dictType 字典类型
     * @return 参数键值
     */
    public List<SysDictData> getType(String dictType) {
        return dictDataService.selectDictDataByType(dictType);
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType  字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    public String getLabel(String dictType, String dictValue) {
        return dictDataService.selectDictLabel(dictType, dictValue);
    }

    /**
     * 根据字典类型和标签值查询字典value
     *
     * @param dictType 字典类型
     * @return 参数键值
     */
    public String getDictValue(String dictType, String dictLabel) {
        List<SysDictData> list = getType(dictType);
        if (CollectionUtils.isEmpty(list)) {
            log.error("无效的字典类型{}", dictLabel);
            return "";
        }
        for (SysDictData sysDictData : list) {
            if (dictLabel.equals(sysDictData.getDictLabel())) {
                return sysDictData.getDictValue();
            }
        }
        log.error("无效的字典标签{}", dictLabel);
        return "";
    }
}
